package clientGUI.userPanels;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;


import clientGUI.userPanels.Controllers.InitiarorPanelController;
import clientserver.Client;
import clientserver.QueryResult;
import college.User;


import java.util.Iterator;
import java.util.List;
import javax.swing.SwingConstants;
import java.awt.Color;

public class InitiatorPanel extends UPanel {
	private JComboBox<String> idCB;
	
private QueryResult req;
private InitiarorPanelController  controller;
private JLabel Statuslbl;
private JComboBox approvedCB;
	
	
	





	
	
	
	public InitiatorPanel(Client c,User u) {
		
	super();	
	setForeground(Color.BLACK);
		
	controller =new InitiarorPanelController(u);

	controller.setClient(c);
		controller.getallusersrid(u);
		 
		 
		
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		
		
		JLabel lblRequestId = new JLabel("Request Id :");
		GridBagConstraints gbc_lblRequestId = new GridBagConstraints();
		gbc_lblRequestId.anchor = GridBagConstraints.EAST;
		gbc_lblRequestId.insets = new Insets(0, 0, 5, 5);
		gbc_lblRequestId.gridx = 0;
		gbc_lblRequestId.gridy = 1;
		add(lblRequestId, gbc_lblRequestId);
		
		
		// Create Combo Box with requests Id's
		idCB = new JComboBox();
		GridBagConstraints gbc_idCB = new GridBagConstraints();
		gbc_idCB.insets = new Insets(0, 0, 5, 5);
		gbc_idCB.fill = GridBagConstraints.HORIZONTAL;
		gbc_idCB.gridx = 1;
		gbc_idCB.gridy = 1;
		
		ArrayList<String> box = new ArrayList<String>();
		box=controller.getallrids();

		Iterator<String> iter=box.iterator();
		String z;
	while(iter.hasNext())
	{
	 z=iter.next();
		idCB.addItem(z);	
	}
		
		add(idCB, gbc_idCB);
		
		JButton btnShowStatus = new JButton("Show Request status");
		btnShowStatus.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			
			{
				String  id=(String) idCB.getSelectedItem();
				String stage=controller.getStagedesctptionbyId(id);
				Statuslbl.setText("Status: "+stage);
				
			}
		});
		GridBagConstraints gbc_btnShowStatus = new GridBagConstraints();
		gbc_btnShowStatus.insets = new Insets(0, 0, 5, 5);
		gbc_btnShowStatus.gridx = 2;
		gbc_btnShowStatus.gridy = 1;
		add(btnShowStatus, gbc_btnShowStatus);
		
		Statuslbl = new JLabel("Status:");
		Statuslbl.setVerticalAlignment(SwingConstants.BOTTOM);
		GridBagConstraints gbc_Statuslbl = new GridBagConstraints();
		gbc_Statuslbl.anchor = GridBagConstraints.WEST;
		gbc_Statuslbl.gridwidth = 3;
		gbc_Statuslbl.insets = new Insets(0, 0, 5, 5);
		gbc_Statuslbl.gridx = 0;
		gbc_Statuslbl.gridy = 2;
		add(Statuslbl, gbc_Statuslbl);
		
		JButton btnNewButton = new JButton("Add change request");
		btnNewButton.addActionListener(new ActionListener() {
		
				public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
				myframe.setChangeRequestPanel();
			}
		});
		GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
		gbc_btnNewButton.insets = new Insets(0, 0, 5, 5);
		gbc_btnNewButton.anchor = GridBagConstraints.ABOVE_BASELINE;
		gbc_btnNewButton.gridx = 1;
		gbc_btnNewButton.gridy = 4;
		add(btnNewButton, gbc_btnNewButton);
		
		JButton btnLogOut = new JButton("Log out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				
				myframe.logout();
			}
		});
		GridBagConstraints gbc_btnLogOut = new GridBagConstraints();
		gbc_btnLogOut.insets = new Insets(0, 0, 5, 5);
		gbc_btnLogOut.gridx = 2;
		gbc_btnLogOut.gridy = 4;
		add(btnLogOut, gbc_btnLogOut);
		
		JButton btnMainMenu = new JButton("Main Menu");
		btnMainMenu.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) 
			{
				myframe.SetMainUserPanel();
			}
		});
		
		JLabel lblRequestsToApprove = new JLabel("Requests to approve :");
		GridBagConstraints gbc_lblRequestsToApprove = new GridBagConstraints();
		gbc_lblRequestsToApprove.gridwidth = 2;
		gbc_lblRequestsToApprove.insets = new Insets(0, 0, 5, 5);
		gbc_lblRequestsToApprove.anchor = GridBagConstraints.EAST;
		gbc_lblRequestsToApprove.gridx = 0;
		gbc_lblRequestsToApprove.gridy = 6;
		add(lblRequestsToApprove, gbc_lblRequestsToApprove);
		
		approvedCB = new JComboBox(controller.getallRidstoAprove().toArray());
		GridBagConstraints gbc_approvedCB = new GridBagConstraints();
		gbc_approvedCB.insets = new Insets(0, 0, 5, 5);
		gbc_approvedCB.fill = GridBagConstraints.HORIZONTAL;
		gbc_approvedCB.gridx = 2;
		gbc_approvedCB.gridy = 6;
		add(approvedCB, gbc_approvedCB);
		
		JButton btnApprove = new JButton("Approve");
		btnApprove.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0)
			{
				String id = (String) approvedCB.getSelectedItem();	
				controller.approveclosure(Integer.parseInt(id));
			}
		});
		GridBagConstraints gbc_btnApprove = new GridBagConstraints();
		gbc_btnApprove.insets = new Insets(0, 0, 5, 5);
		gbc_btnApprove.gridx = 4;
		gbc_btnApprove.gridy = 6;
		add(btnApprove, gbc_btnApprove);
		GridBagConstraints gbc_btnMainMenu = new GridBagConstraints();
		gbc_btnMainMenu.insets = new Insets(0, 0, 0, 5);
		gbc_btnMainMenu.gridx = 2;
		gbc_btnMainMenu.gridy = 9;
		add(btnMainMenu, gbc_btnMainMenu);

	}





















	

}
